import { Message } from 'element-ui'

const axios = require('axios')

const baseUrl = 'http://localhost:8080'

const request = axios.create({
  baseURL: baseUrl,
  timeout: 5000
})

request.interceptors.response.use(
  res => {
    switch (true) {
      // case res.status === 200:
      //   console.log('res:' + res)
      //   return res.data
      // case !!res.status:
      //   Message.error({
      //     dangerouslyUseHTMLString: true,
      //     message: `<pre>${res.data.message}</pre>`
      //   })
      //   return Promise.reject(res.data)
      default:
        console.log('res:'+res)
        console.log('res data:'+res.data)
        return res.data
    }
  },
  error => {
    handleErrorRequest(error)
    return Promise.reject(error)
  }
)
const handleErrorRequest = () => {
  Message.error('网络超时')
}

export default {
  post (url = '', data = {}, config = {}) {
    return request.post(url, data, config)
  },
  put (url = '', data = {}, config = {}) {
    return request.put(url, data, config)
  },
  patch (url = '', data = {}, config = {}) {
    return request.patch(url, data, config)
  },
  get (url = '', params = {}, config = {}) {
    const OPTIONS = Object.assign({ params }, config)
    return request.get(url, OPTIONS)
  },
  delete (url = '', params = {}, config = {}) {
    const OPTIONS = Object.assign({ params }, config)
    return request.delete(url, OPTIONS)
  }
}
